<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>实时聊天应用 - 注册</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3B82F6',
                        secondary: '#10B981',
                        dark: '#1E293B',
                        light: '#F8FAFC'
                    },
                    fontFamily: {
                        sans: ['Inter', 'system-ui', 'sans-serif'],
                    },
                }
            }
        }
    </script>
</head>
<body class="bg-gray-100 font-sans text-gray-800">
<!-- 注册页面 -->
<div id="register-page" class="min-h-screen flex items-center justify-center bg-gradient-to-br from-blue-50 to-indigo-100 p-4">
    <div class="w-full max-w-md">
        <div class="bg-white rounded-xl shadow-lg overflow-hidden transform">
            <div class="p-6 sm:p-8">
                <div class="text-center mb-8">
                    <h2 class="text-[clamp(1.5rem,3vw,2rem)] font-bold text-gray-900">
                        注册账号
                    </h2>
                    <p class="mt-2 text-gray-500">创建账号以使用实时聊天功能</p>
                </div>

                <form id="register-form" class="space-y-6">
                    <div>
                        <label for="reg-name" class="block text-sm font-medium text-gray-700 mb-1">用户名</label>
                        <div class="relative">
                            <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                                <i class="fa fa-user text-gray-400"></i>
                            </div>
                            <input type="text" id="reg-name" name="reg-name" required
                                   class="pl-10 block w-full rounded-lg border-gray-300 shadow-sm border focus:border-primary focus:ring focus:ring-primary/20 transition-all duration-200 px-4 py-2.5"
                                   placeholder="请输入用户名">
                        </div>
                    </div>

                    <div>
                        <label for="reg-password" class="block text-sm font-medium text-gray-700 mb-1">密码</label>
                        <div class="relative">
                            <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                                <i class="fa fa-lock text-gray-400"></i>
                            </div>
                            <input type="password" id="reg-password" name="reg-password" required
                                   class="pl-10 block w-full rounded-lg border-gray-300 shadow-sm border focus:border-primary focus:ring focus:ring-primary/20 transition-all duration-200 px-4 py-2.5"
                                   placeholder="请输入密码">
                        </div>
                    </div>

                    <div>
                        <label for="reg-password1" class="block text-sm font-medium text-gray-700 mb-1">确认密码</label>
                        <div class="relative">
                            <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                                <i class="fa fa-lock text-gray-400"></i>
                            </div>
                            <input type="password" id="reg-password1" name="reg-password1" required
                                   class="pl-10 block w-full rounded-lg border-gray-300 shadow-sm border focus:border-primary focus:ring focus:ring-primary/20 transition-all duration-200 px-4 py-2.5"
                                   placeholder="确认密码">
                        </div>
                    </div>

                    <div>
                        <label for="reg-email" class="block text-sm font-medium text-gray-700 mb-1">邮箱</label>
                        <div class="relative">
                            <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                                <i class="fa fa-envelope text-gray-400"></i>
                            </div>
                            <input type="email" id="reg-email" name="reg-email" required
                                   class="pl-10 block w-full rounded-lg border-gray-300 shadow-sm border focus:border-primary focus:ring focus:ring-primary/20 transition-all duration-200 px-4 py-2.5"
                                   placeholder="请输入邮箱">
                        </div>
                    </div>

                    <div>
                        <label for="reg-phone" class="block text-sm font-medium text-gray-700 mb-1">手机号</label>
                        <div class="relative">
                            <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                                <i class="fa fa-phone text-gray-400"></i>
                            </div>
                            <input type="tel" id="reg-phone" name="reg-phone" required
                                   class="pl-10 block w-full rounded-lg border-gray-300 shadow-sm border focus:border-primary focus:ring focus:ring-primary/20 transition-all duration-200 px-4 py-2.5"
                                   placeholder="请输入手机号">
                        </div>
                    </div>

                    <div>
                        <button type="submit"
                                class="w-full flex justify-center py-2.5 px-4 border border-transparent rounded-lg shadow-sm text-sm font-medium text-white bg-primary hover:bg-primary/90 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary transition-all duration-200 transform hover:scale-[1.02] active:scale-[0.98]">
                            注册
                        </button>
                    </div>
                </form>

                <div id="register-error" class="mt-4 text-center text-red-500 text-sm hidden">
                    注册失败，请重试
                </div>

                <div id="register-success" class="mt-4 text-center text-green-500 text-sm hidden">
                    注册成功，即将跳转到登录页面...
                </div>

                <div class="mt-6 text-center">
                    <p class="text-gray-600 text-sm">
                        已有账号？ <button id="show-login-btn" class="text-primary hover:text-primary/80 font-medium">立即登录</button>
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    const BACK_ADDRESS_PORT = '47.121.204.23:8081';
    // DOM元素
    const registerForm = document.getElementById('register-form');
    const registerError = document.getElementById('register-error');
    const registerSuccess = document.getElementById('register-success');
    const showLoginBtn = document.getElementById('show-login-btn');

    // 页面初始化
    document.addEventListener('DOMContentLoaded', () => {
        // 事件监听
        registerForm.addEventListener('submit', handleRegister);
        showLoginBtn.addEventListener('click', () => {
            window.location.href = 'login.html';
        });
    });

    // 注册处理
    async function handleRegister(e) {
        e.preventDefault();

        const name = document.getElementById('reg-name').value;
        const password = document.getElementById('reg-password').value;
        const rePassword = document.getElementById('reg-password1').value;
        const email = document.getElementById('reg-email').value;
        const phone = document.getElementById('reg-phone').value;

        // 前端验证密码一致性
        if (password !== rePassword) {
            registerError.textContent = '两次输入的密码不一致';
            registerError.classList.remove('hidden');
            return;
        }

        // 构建查询参数
        const params = new URLSearchParams();
        params.append('name', name);
        params.append('password', password);
        params.append('rePassword', rePassword);
        params.append('email', email);
        params.append('phone', phone);

        try {
            // 调用GET请求的注册接口
            const response = await fetch('http://' + BACK_ADDRESS_PORT +`/user/createUser?${params.toString()}`, {
                method: 'GET',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            });

            const result = await response.json();

            if (response.ok && result.code === 200) {
                // 注册成功
                registerError.classList.add('hidden');
                registerSuccess.classList.remove('hidden');

                // 3秒后自动跳转到登录页面
                setTimeout(() => {
                    window.location.href = 'login.html';
                }, 3000);
            } else {
                // 注册失败
                registerSuccess.classList.add('hidden');
                registerError.textContent = result.message || '注册失败，请重试';
                registerError.classList.remove('hidden');
            }
        } catch (error) {
            console.error('注册失败:', error);
            registerSuccess.classList.add('hidden');
            registerError.textContent = '网络错误，请重试';
            registerError.classList.remove('hidden');
        }
    }
</script>
</body>
</html>